package com.class05.controller;

import com.class05.pojo.Schedule;
import com.class05.service.ScheduleService;
import com.class05.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassDescription:
 * @JdkVersion: 17
 * @Author: yxy
 * @Created: 2023/12/22 16:22
 */
@CrossOrigin//允许其他资源访问我们的
//controller 浏览器大哥就不拦截了！
@RestController
@RequestMapping("schedule")
@Slf4j
public class ScheduleController {
    @Autowired
    private ScheduleService scheduleService;
    @GetMapping("/{pageSize}/{currentPage}")
    public R page(@PathVariable int pageSize,@PathVariable int currentPage){
        R r = scheduleService.page(pageSize, currentPage);
        log.info("查询的数据为：{}",r);
        return r;
    }
    @DeleteMapping("/{id}")
    public R remove(@PathVariable int id){
        R r = scheduleService.remove(id);
        return r;
    }
    @PostMapping
    public R save(@Validated @RequestBody Schedule schedule, BindingResult result){
        if (result.hasErrors()){
            R.fail("参数为null，不能保存");
        }
        R r = scheduleService.save(schedule);
        return r;
    }

    @PutMapping
    public R update(@Validated @RequestBody Schedule schedule, BindingResult result){
        if (result.hasErrors()){
            R.fail("参数为null，不能修改");
        }
        R r = scheduleService.update(schedule);
        return r;
    }
}
